package game.resources;

import game.enumCompany;
import org.apache.ecs.xml.XML;
import server.DBLogic;

public class LogWriter {
	
    private final static String SEPARATOR = "^";
    
	private String[] playerNames;
	
	private int gameID;

	private String msg;
	private String privateMsg;
	private long toPlayerID;
	
	private boolean existMsg;
	private boolean existPrivateMsg;
	
	public LogWriter(int gameID,String[] playerNames) {
		this.gameID = gameID;
		this.playerNames = playerNames;
		msg = "";
		privateMsg = "";
		existMsg = false;
		existPrivateMsg = false;
		toPlayerID = 0;
	}
	
	public void printLogMessage(int msg,int ...args){
		switch(msg)
		{
		case 1:
			stackMsg(playerNames[args[0]] + " has no legal moves to make! His turn will be skipped, but he may buy stocks.\n");
			break;
		case 3:
			stackMsg("No free share is given since there are no more available shares left in the bank.\n");
			break;
		case 4:
			stackMsg("Could not fetch a new tile - There are no more tiles in the bank!\n");
			break;
		case 11:
			stackMsg(playerNames[args[0]] + " sold " + args[1] + " shares for $" + args[2] + ".\n");
			break;
		case 12:
			stackMsg(playerNames[args[0]] + " converted " + args[1] + " shares into " + args[2] + " " + enumCompany.values()[args[3]].toString() + " shares.\n");
			break;
		case 13:
			stackMsg(playerNames[args[0]]+ " held on to " + args[1] + " shares.\n");
			break;
		case 14:
			stackMsg(playerNames[args[0]] + " sold " + args[1] + " for $" + args[2] + ".\n");
			break;
		case 16:
			stackMsg("Company " + enumCompany.values()[args[0]].toString() + " is sold to the bank.");
			break;
		case 17:
			stackMsg("Company " + enumCompany.values()[args[0]].toString() + " is sold to company " + enumCompany.values()[args[1]].toString() + ".\n");
			break;
		case 18:
			stackMsg("First Prize Wins:");
			break;
		case 19:
			stackMsg(playerNames[args[0]] + " gets $" + args[1]);
			break;
		case 20:
			stackMsg("Second Prize Wins:"); 
			break;
		case 21:
			stackMsg(playerNames[args[0]] + " gets $" + args[1] +".\n");
			break;
		case 22:
			stackMsg("Share Holders:");
			break;
		case 23:
			stackMsg(playerNames[args[0]] + ", has " + args[1] + " shares of " + 
					enumCompany.values()[args[2]].toString() + ".");
			break;
		case 30:
			stackMsg("It&#39;s "+playerNames[args[0]]+"&#39;s turn."); 
			break;
		case 31:
			stackMsg(playerNames[args[0]]+" just bought "+ args[1] +" shares of "+ enumCompany.values()[args[2]].toString() + ".");	
			break;
		case 32:
			stackMsg(playerNames[args[0]]+ " placed tile " +((char)(args[1]+65))+""+(args[2]+1) + ".");
			break;
		case 33:
			stackMsg(playerNames[args[0]]+ " did not have any tile to place on board.");
			break;
		case 34:
			stackMsg(playerNames[args[0]]+ " did not buy any shares.");
			break;
		case 35:
			stackMsg("and also established company "+ enumCompany.values()[args[0]].toString() + ".");
			break;
		} 

	}
	
	public void printLogPrivateMessage(long playerID,int msg,int ...args){
		switch(msg)
		{
		case 2:
			stackPrivateMsg(playerID,"Notice: The tile you have placed is in between two safe companies.\nChoose one more tile to place.\n");
			break;
		case 5:
			stackPrivateMsg(playerID,"Transaction completed.\n$" + args[0] + " were withdrawn from your bank account.\n");
			break;
		case 6:
			stackPrivateMsg(playerID,"You do not have enough money - you are $" + args[0] + " short.\n");
			break;
		case 9:
			stackPrivateMsg(playerID,"There are only " + args[0] + " shares of " + enumCompany.values()[args[1]].toString() + " left to convert to.\n");
			break;
		case 10:
			stackPrivateMsg(playerID,"There are only " + args[0] + " shares of " + enumCompany.values()[args[1]].toString() + " left to buy.\n");
			break;
		case 15:
			stackPrivateMsg(playerID,"Error: Can not place a tile which creates a company when all companies are on board.\nPick a different tile.");
			break;
		case 36:
			stackPrivateMsg(playerID,"Your current rating is: " + args[0]);
			break;
		}
	}
	
	private void stackMsgHelper(Boolean isPrivate,String stackedMsg,String msg) {
		stackedMsg += msg;
		stackedMsg += SEPARATOR;
		if (isPrivate)
			this.privateMsg = stackedMsg;
		else 
			this.msg = stackedMsg;	
	}
	
	private void stackMsg(String msg) {
		stackMsgHelper(false,this.msg,msg);
		this.existMsg = true;
	}
	
	private void stackPrivateMsg(long playerID,String msg) {
		stackMsgHelper(true,this.privateMsg,msg);
		if (!existPrivateMsg) {
			this.toPlayerID = playerID;
		}
		this.existPrivateMsg = true;
	}
	
	public void postLogMessage() {
		if (existMsg) {
			postLogMessage(0,msg,false);
			existMsg = false;
			msg = "";
		}
		if (existPrivateMsg) {
			postLogMessage(toPlayerID,privateMsg,true);
			existPrivateMsg = false;
			privateMsg = "";
			toPlayerID = 0;
		}
	}
	
	private void postLogMessage(long userID,String msg,boolean isPrivate) {
		XML post = new XML("post");
		XML userIDXML = new XML("userID");
		XML time = new XML("time");
		time.addElement(Long.toString(System.currentTimeMillis()));
		userIDXML.addElement(Long.toString(userID));
		XML entry = new XML("entry");
		entry.addElement(msg);
		post.addElement(userIDXML);
		post.addElement(entry);
		post.addElement(time);
		DBLogic dbl = new DBLogic();
		dbl.InitDBConnection();
		if (isPrivate)
			dbl.postPrivateGameLog(gameID, post.toString());
		else
			dbl.postGameLog(gameID, post.toString());
		dbl.ShutDBConnection();
	}
}
